﻿using BCL.BaseLib;
using BCL.BaseLib.Basics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BCL.Tool;
namespace BCL.WPFComponent
{
    /// <summary>
    /// UserControl3.xaml 的交互逻辑
    /// </summary>
    public partial class TabHeadItem : UserControl, IFunctionControl
    {
        private bool isSelect;
        private FunctionEntity functionEntity;
        private FunctionControlSetting setting;
        private List<ImageSource> images;
        private event Action<TabHeadItem> selectChanged;
        public event Action<TabHeadItem> SelectChanged
        {
            add { selectChanged += value; }
            remove { selectChanged -= value; }
        }

        private event Action<TabHeadItem> closeIng;
        public event Action<TabHeadItem> CloseIng
        {
            add { closeIng += value; }
            remove { closeIng -= value; }
        }

        public TabHeadItem()
        {
            InitializeComponent();
        }

        public TabHeadItem(FunctionEntity fe)
        {
            InitializeComponent();
            functionEntity = fe;
            if (!string.IsNullOrEmpty(fe.ImagePath))
            {
                images = new List<ImageSource>();
                string path1 = fe.ImagePath.Insert(fe.ImagePath.LastIndexOf("."), "_0");
                images.Add(new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\" + path1)));
                path1 = fe.ImagePath.Insert(fe.ImagePath.LastIndexOf("."), "_1");
                images.Add(new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\" + path1)));
                text1.Text = fe.Title?.Language();
            }
        }

        public bool IsSelect
        {
            get { return isSelect; }

            set
            {

                if (isSelect == value) return;
                isSelect = value;
                if (value)
                {
                    TextBlock1.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("White"));
                    text1.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("Black"));
                    Image1.Source = images?[0];
                }
                else
                {
                    TextBlock1.Background =FindResource("MenuHeadBorderBrush") as SolidColorBrush;
                    text1.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("White"));
                    Image1.Source = images?[1];
                }
            }
        }

        public void Close()
        {
            closeIng?.Invoke(this);
        }
        public FunctionControlSetting Setting { get => setting; set => setting = value; }
        public FunctionEntity Function { get => functionEntity; set => functionEntity = value; }

        public void LanguageBinding()
        {
            throw new NotImplementedException();
        }

        public void ReloadData(object data)
        {
            throw new NotImplementedException();
        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (!isSelect)
            {
                IsSelect = true;
                selectChanged?.Invoke(this);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.Grid1.DataContext = functionEntity;
        }

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Close();
        }
    }
}
